package com.apes.scm.stock.inventory.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * 盘点表明细
 * @author yiny
 * @create 2018.9.15
 *
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InventoryListItem.CONST_TABLE_NAME)
public class InventoryListItem extends BaseModel{
    public static final String CONST_TABLE_NAME = "sto_inventory_list_item";

    @Id
    @Column(name = "id")
    @GenericGenerator(name = "id-generator", strategy = "uuid")
    @GeneratedValue(generator = "id-generator")
//    @ApesGenerator(code = CONST_TABLE_NAME, name = "盘点明细序列", padding = 1)
    private String id; // 唯一编码

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inventoryList_id", referencedColumnName = "id")
    private InventoryList inventoryListId; // 盘点单号

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "product_id", referencedColumnName = "id", nullable = false)
    private Product productId;  // 商品编码

//    @Transient
//    private String product;
//
//    public String getProduct() {
//        if(productId == null) return null;
//        return productId.getId();
//    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "unit_id", referencedColumnName = "id")
    private ProductUom unitId;  // 计量单位

    @Column(name = "usable_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double usableQty = 0; // 可用数

    @Column(name = "custody_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double custodyQty = 0; // 保管数

     @Column(name = "intransit_qty", columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double inTransitQty = 0; // 在途(领用在途 + 转储发货在途)

    @Column(name = "count_quantity", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double countQuantity = 0; //盘点数量

    @Column(name = "profit_and_loss", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double profitAndLoss = 0; // 盈亏数量


    public String acquireProductCode() {
        return productId.getId();
    }


}
